/**
 * <pre>
 * Title: 		AccountSerivceImpl.java
 * Project: 	MSTS
 * Author:		pengfangliang
 * Create:	 	2012-6-18 下午04:16:15
 * Copyright: 	Copyright (c) 2012
 * Company:		Shenzhen Helper
 * <pre>
 */
package com.hengpeng.msls.service.account.impl;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

import jxl.biff.drawing.Drawing;

import org.springframework.stereotype.Service;

import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.Restriction;
import com.hengpeng.common.persist.query.Restrictions;
import com.hengpeng.common.persist.query.model.Order;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.itfinbase.persist.Account;
import com.hengpeng.itfinbase.persist.AccountLog;
import com.hengpeng.itfinbase.persist.AccountLog.TransType;
import com.hengpeng.itfinbase.persist.Payment;
import com.hengpeng.itfinbase.persist.Payment.PayStatus;
import com.hengpeng.msls.port.client.vo.client.AccountTransType;
import com.hengpeng.msls.service.BaseService;
import com.hengpeng.msls.service.account.AccountService;

/**
 * <pre>
 *  资金服务接口实现
 * </pre>
 * @author pengfangliang
 * @version 1.0, 2012-6-18
 */
@Service
public class AccountSerivceImpl extends BaseService implements AccountService
{

	@Override
	public Account getAccount(Long userId)
	{
		DynamicQuery query = new DynamicQuery(Account.class);
		query.eq("userId", userId);
		return serviceFactory.getProtFactory().getEntity(query, Account.class);
	}

	/**
	 * <pre>
	 *  分页查询提现明细
	 * </pre>
	 * @param betcardNo 投注号码
	 * @param beginRequestTime 开始时间
	 * @param endRequestTime 截止时间
	 * @param state 处理状态
	 * @param pageIndex 当前页号
	 * @param pageSize 每页记录数
	 * @return
	 */
	@Override
	public List<Drawing> findDrawingWithPage(String betcardNo, Date beginRequestTime, Date endRequestTime,
			PayStatus state, Integer pageIndex, Integer pageSize)
	{
		return null;
	}

	/**
	 * <pre>
	 * 分页查询提现明细
	 * </pre>
	 * @param pageIndex 当前页号
	 * @param pageSize 每页记录数
	 * @param sortord 排序方式
	 * @param rstcn4and 对提现实体的与条件
	 * @return
	 */
	@Override
	public QueryResult<Drawing> findDrawingByPage(Integer pageIndex, Integer pageSize,
			LinkedHashMap<String, OrderType> sortord, Restriction... rstcn4and)
	{
		return null;
	}

	/**
	 * <pre>
	 *   查询总记录数
	 * </pre>
	 * @param betcardNo 投注卡号
	 * @param beginRequestTime 开始请求时间
	 * @param endRequestTime 截止请求时间
	 * @param state 状态
	 * @return
	 */
	@Override
	public Long count(String betcardNo, Date beginRequestTime, Date endRequestTime, PayStatus state)
	{
		return null;
	}

	/**
	 * <pre>
	 *  动态分页查询用户资金明细流水
	 * </pre>
	 * @param userName 用户名
	 * @param beginTransTime 开始交易时间
	 * @param endTransTime 截止交易时间
	 * @param transType 交易类型
	 * @param pageIndex 当前页号
	 * @param pageSize 每页记录数
	 * @return
	 */
	@Override
	public List<AccountLog> findAccountLogWithPage(String userName, Date beginTransTime, Date endTransTime,
			TransType[] transType, Integer pageIndex, Integer pageSize)
	{
		DynamicQuery query = new DynamicQuery(AccountLog.class);
		query.add(Restrictions.eq("userName", userName));
		if (transType != null && transType.length > 0)
		{
			query.add(Restrictions.inArray("transType", transType));
		}
		if (beginTransTime != null && endTransTime != null)
		{
			query.add(Restrictions.between("transTime", beginTransTime, endTransTime));
		}
		if (beginTransTime != null && endTransTime == null)
		{
			query.add(Restrictions.ge("transTime", beginTransTime));
		}
		if (beginTransTime == null && endTransTime != null)
		{
			query.add(Restrictions.le("transTime", endTransTime));
		}
		query.setPageIndex(pageIndex);
		query.setPageSize(pageSize);
		query.desc("transTime");
		return serviceFactory.getProtFactory().queryList(query, AccountLog.class);
	}

	@Override
	public Long findAccountLogWithCount(String userName, Date beginTransTime, Date endTransTime, TransType[] transType)
	{
		DynamicQuery query = new DynamicQuery(AccountLog.class);
		query.add(Restrictions.eq("userName", userName));
		if (transType != null && transType.length > 0)
		{
			query.add(Restrictions.inArray("transType", transType));
		}
		if (beginTransTime != null && endTransTime != null)
		{
			query.add(Restrictions.between("transTime", beginTransTime, endTransTime));
		}
		if (beginTransTime != null && endTransTime == null)
		{
			query.add(Restrictions.ge("transTime", beginTransTime));
		}
		if (beginTransTime == null && endTransTime != null)
		{
			query.add(Restrictions.le("transTime", endTransTime));
		}
		query.addProjection(Projections.count());
		return serviceFactory.getProtFactory().count(AccountLog.class, query);
	}

	@Override
	public QueryResult<Payment> findPaymentByPage(Integer pageIndex, Integer pageSize,
			LinkedHashMap<String, OrderType> sortord, String betCardNo, Date beginRequestTime, Date endRequestTime)
	{
		return null;
	}

	@Override
	public Integer findDrawingSumByParam(String betCardNo, String refrenceId)
	{
		return null;
	}

	@Override
	public QueryResult<AccountLog> findAccountLogByPage(Integer pageIndex, Integer pageSize,
			LinkedHashMap<String, OrderType> sortord, Restriction... rstcn4and)
	{
		DynamicQuery query = new DynamicQuery(AccountLog.class);
		query.add(Restrictions.and(rstcn4and));
		if (sortord != null)
		{
			for (Entry<String, OrderType> iterable_element : sortord.entrySet())
			{
				if (iterable_element.getValue() == Order.OrderType.ASC)
				{
					query.asc(iterable_element.getKey());
				}
				else
				{
					query.desc(iterable_element.getKey());
				}
			}
		}
		else
		{
			query.desc("transTime");
		}
		query.setPageIndex(pageIndex);
		query.setPageSize(pageSize);
		query.setIsSearchTotalCount(true);
		return serviceFactory.getProtFactory().remoteQuery(query, AccountLog.class);
	}

	@Override
	public List<Object> findAccountLogMoneySta(String betCardNo, Date beginTransTime, Date endTransTime)
	{
		DynamicQuery query = new DynamicQuery(AccountLog.class, "al");
		query.addProjection(Projections.property("al.transType"));
		query.addProjection(Projections.property("al.tradeMode"));
		query.addProjection(Projections.sum("al.transBaseMoney"));
		query.addProjection(Projections.sum("al.transBonusMoney"));
		query.addProjection(Projections.sum("al.transPresentMoney"));
		query.eq("al.betCardNo", betCardNo);
		query.ge("transTime", beginTransTime);
		query.le("transTime", endTransTime);
		query.addGourp("al.transType");
		query.addGourp("al.tradeMode");
		return serviceFactory.getProtFactory().findObject(null, query);
	}

	@Override
	public List<AccountLog> findAccountLogByProperty(String betCardNo, String note, AccountTransType[] transType)
	{
		DynamicQuery query = new DynamicQuery(AccountLog.class);
		query.add(Restrictions.eq("betCardNo", betCardNo));
		query.add(Restrictions.eq("note", note));
		query.add(Restrictions.inArray("transType", transType));
		query.desc("transTime");
		return serviceFactory.getProtFactory().queryList(query, AccountLog.class);
	}

	public AccountLog findAccountLog(String referenceId, AccountTransType type)
	{
		DynamicQuery query = new DynamicQuery(AccountLog.class);
		query.eq("referenceId", referenceId);
		query.eq("transType", type);
		query.desc("transTime");
		query.setPageSize(1);
		return serviceFactory.getProtFactory().getEntity(query, AccountLog.class);
	}
}
